package com.hodgepodge.commons.base.entity;

import com.hodgepodge.commons.enums.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * title Result
 * projectName colorful-blog
 * desc 自定义通用响应类
 *
 * @author jiangjian
 * @date 2021/2/2 15:00
 */
@Getter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(description = "通用响应类")
public class Result<T> {

    /**
     * 响应状态码
     */
    @ApiModelProperty(value = "响应状态码", required = true)
    private int code;

    /**
     * 响应信息
     */
    @ApiModelProperty(value = "响应信息", required = true)
    private String msg;

    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    private T data;

    /**
     * @param resultCodeEnum 响应码枚举类
     */
    private Result(ResultCodeEnum resultCodeEnum) {
        this(resultCodeEnum.code, resultCodeEnum.msg, null);
    }

    /**
     * @param resultCodeEnum 响应码枚举类
     * @param msg            响应信息
     */
    private Result(ResultCodeEnum resultCodeEnum, String msg) {
        this(resultCodeEnum, msg, null);
    }

    /**
     * @param resultCodeEnum 响应码枚举类
     * @param data           响应数据
     */
    private Result(ResultCodeEnum resultCodeEnum, T data) {
        this(resultCodeEnum, resultCodeEnum.msg, data);
    }

    /**
     * @param resultCodeEnum 响应码枚举类
     * @param msg            响应信息
     * @param data           响应数据
     */
    private Result(ResultCodeEnum resultCodeEnum, String msg, T data) {
        this(resultCodeEnum.code, msg, data);
    }

    /**
     * @param <T> 响应数据
     */
    public static <T> Result<T> builder() {
        return new Result<>();
    }

    /**
     * @param <T> T 响应数据
     */
    public static <T> Result<T> success() {
        return new Result<>(ResultCodeEnum.SUCCESS);
    }

    /**
     * @param msg 响应信息
     * @param <T> T 响应数据
     */
    public static <T> Result<T> success(String msg) {
        return new Result<>(ResultCodeEnum.SUCCESS, msg);
    }

    /**
     * @param data 响应数据
     * @param <T>  T 响应数据
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCodeEnum.SUCCESS, data);
    }

    /**
     * @param msg  响应信息
     * @param data 响应数据
     * @param <T>  T 响应数据
     */
    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(ResultCodeEnum.SUCCESS, msg, data);
    }

    /**
     * @param <T> T 响应数据
     */
    public static <T> Result<T> fail() {
        return new Result<>(ResultCodeEnum.FAILED);
    }

    /**
     * @param msg 响应信息
     * @param <T> T 响应数据
     */
    public static <T> Result<T> fail(String msg) {
        return new Result<>(ResultCodeEnum.FAILED, msg);
    }

    /**
     * @param <T> T 响应数据
     */
    public static <T> Result<T> error() {
        return new Result<>(ResultCodeEnum.ERROR);
    }

    /**
     * @param msg 响应信息
     * @param <T> T 响应数据
     */
    public static <T> Result<T> error(String msg) {
        return new Result<>(ResultCodeEnum.ERROR, msg);
    }

    /**
     * @param flag true:成功 false:失败
     * @param <T>  T 响应数据
     */
    public static <T> Result<T> result(boolean flag) {
        return flag ? Result.success() : Result.fail();
    }

    /**
     * @param code 状态码
     * @param msg  响应信息
     * @param data 响应数据
     * @param <T>  T 响应数据
     */
    public static <T> Result<T> result(int code, String msg, T data) {
        return new Result<>(code, msg, data);
    }

    /**
     * @param resultCodeEnum 响应码枚举类
     * @param <T>            T 响应数据
     */
    public static <T> Result<T> result(ResultCodeEnum resultCodeEnum) {
        return new Result<>(resultCodeEnum);
    }

}
